﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.IO;
using com.YiXiong.Util;
namespace Loong.Net
{
    public class Request : IRequest
    {
        // Create a request for the URL. 		
        private WebRequest request;

        private string html = string.Empty;

        private Encoding encoding;


        public Request(string url)
        {
            // Console.Write(url);             
            encoding = Encoding.Default;
            request = WebRequest.Create(url);
        }
        /// <summary>
        /// 设置编码
        /// 用例:reqeust.Encodings = Encoding.Default;
        /// </summary>
        public Encoding Encodings
        {
            get
            {
                return this.encoding;
            }
            set
            {
                this.encoding = value;
            }
        }

        /// <summary>
        /// 添加用例:request.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible;  Windows 5.2)");   
        /// </summary>
        public WebHeaderCollection Header
        {
            get
            {
                return this.request.Headers;
            }
            set
            {
                this.request.Headers = value;
            }
        }

        public string HTML
        {
            get
            {
                return this.html;
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public string LocalPath
        {
            get { return request.RequestUri.LocalPath; }
        }

        public string Query
        {
            get { return request.RequestUri.Query; }
        }

        public int SendTimeout
        {
            get
            {
                return request.Timeout;
            }
            set
            {
                request.Timeout = value;
            }
        }

        public bool Get()
        {
            // Get the response.

            try
            {


                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                request.Headers.Add("HTTP_REFERER", "http://QQ.COM");
                enEncoding(response);
                // Display the status.
                //Console.WriteLine(response.StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream, encoding);
                this.html = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();
                response.Close();
                return true;
            }
            catch (System.Net.WebException e)
            {
                return false;
            }

        }



        private void enEncoding(WebResponse w)
        {
            if (encoding != null)
            {
                return;
            }
            string charset = com.YiXiong.Util.Net.DeEncoding(w);

            if (!string.IsNullOrEmpty(charset))
            {
                try
                {
                    encoding = Encoding.GetEncoding(charset);
                }
                catch
                {
                    encoding = Encoding.Default;
                }
            }
        }









        #region IRequest 成员


        public void Close()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
